﻿#define SHOW_CONSOLE
#include <graphics.h>
#include <iostream>
#include <Windows.h>
#include "Scene.h"
#include "ByteImage.h"
#include "RawImage.h"
#include "Tracer.h"
#include "SceneDefinations.h"

using namespace std;

void displayRecall(int x, int y, Color & c) {
	const Float invGamma = 1 / 2.2f;
	putpixel(x, y, c.gammaCorrect_inv(invGamma).toRGBInt());
}

void render(Image & image) {
	cout << "Building Scene..." << endl;
	Scene scene;
	TestScenes::CornellBox_LargeLight_Mirror(scene);

	Tracer<MultiJitteredSampler> tracer(scene, image);
	tracer.setSampleCount(16);
	tracer.setInstantDisplayRecall(displayRecall);

	cout << "Rendering Scene(" << tracer.getSPP() << " spp";
	PathIntegrator * pi = dynamic_cast<PathIntegrator*>(scene.getIntegrator().get());
	if (pi) cout << ", " << pi->getGISPP() << " GI spp";
	cout << ")..." << endl;
	long long start = GetTickCount64();
	tracer.render();
	cout << "Rendering Completed! Time Used:" << GetTickCount64() - start << "ms" << endl;
}

void drawImage(const Image &image) {
	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			putpixel(x, y, image.getOutputColor_int(x, y));
		}
	}
}

void main() {
	if (!SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS)) {
		//if (!ERROR_PROCESS_MODE_ALREADY_BACKGROUND == GetLastError())
			cout << "Priority Setting Failed!" << endl;
	}

	setinitmode(0);
	initgraph(700, 700);
	setcaption("LightingRender");

	RawImage image(700, 700);
	render(image);

	//drawImage(image);

	while (is_run()) {
		ostringstream s;
		s << "LightingRender Fps:" << getfps();
		setcaption(s.str().c_str());
		delay_fps(60);
	}
}